import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;

public class NetConnectTest {

	public static void main(String[] args) {
		NetConnectTest nt = new NetConnectTest();
		// nt.doTest();
		nt.login("duyun", "a");
	}

	public String login(String userName, String psw) {
		try {
			URL url = new URL("http://localhost:8080/adcrm/crm/2.0/login_action.jsp");
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setRequestMethod("POST");
			httpConn.setFollowRedirects(false);
			httpConn.setInstanceFollowRedirects(false);
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			OutputStream os = httpConn.getOutputStream();
			String s = "username=" + userName + "&password=" + psw;
			os.write(s.getBytes());
			os.flush();
			os.close();
			displayInputStream(httpConn.getInputStream(), "UTF-8");
			
			String cookies = getCookies(httpConn);
			System.out.println(cookies);

			
//			System.out.println("the URL has move to " + httpConn.getHeaderField("Location"));
//			httpConn.disconnect();
//
//			URL newURL = new URL(httpConn.getHeaderField("Location"));
//
//			httpConn = (HttpURLConnection) newURL.openConnection();
//			httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
//			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//			httpConn.setRequestProperty("Cookie", cookies);
//			httpConn.setDoInput(true);
//			displayInputStream(httpConn.getInputStream(), "UTF-8");

			return cookies;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public void doTest() {
		try {

			URL url = new URL(
					"http://10.10.221.127:7001/analytics/saw.dll?Go&Path=/users/liwei/Inventory_Max_Min_level&NQUser=liwei&NQPassword=liwei123&Action=Navigate&P0=6&P1=bet&P2=%22BIEE_BB_DATE%22.%22THE_DAY%22&P3=2+2012-03-08+2012-03-18&P4=lt&P5=%22LEVEL_PRIORITY%22.%22LEVEL_PRIORITY%22&p6=1+688&P7=eq&P8=%22BI_TD_BB_POS_GROUP_POS_GROUP_2%22.%22AD_BLK_ID%22&p9=2+2+3&P10=eq&P11=%22BIEE_TD_AREA_PROVINCE_CITY%22.%22CITY_ID%22&P12=2+150500+150600&P16=eq&P17=%22BIEE_TD_CHANNEL_SUBCHANNEL%22.%22CHANNEL_ID%22&p18=2+c+d&Format=XML");
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setRequestMethod("POST");
			httpConn.setFollowRedirects(false);
			httpConn.setInstanceFollowRedirects(false);
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			OutputStream os = httpConn.getOutputStream();
			// String s = "username=wangbin&password=12";
			// os.write(s.getBytes());
			os.flush();
			os.close();
			displayInputStream(httpConn.getInputStream(), "UTF-8");
			System.out.println("---------------------------------------");

			String cookies = getCookies(httpConn);

			System.out.println(cookies);

			System.out.println("----------------------------------------");
			httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Cookie", cookies);
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			os = httpConn.getOutputStream();
			os.flush();
			os.close();
			displayInputStream(httpConn.getInputStream(), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getCookies(HttpURLConnection conn) {
		StringBuffer cookies = new StringBuffer();
		String headName;
		for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {
			StringTokenizer st = new StringTokenizer(headName, "; ");
			while (st.hasMoreTokens()) {
				cookies.append(st.nextToken() + "; ");
			}
		}
		return cookies.toString();
	}

	private void displayInputStream(InputStream is, String encoding) {
		Scanner sc = new Scanner(is, encoding);
		while (sc.hasNextLine()) {
			System.out.println(sc.nextLine());
		}
	}

}
